// 移除import语句以便检查语法
const authService = {};
const chatRoomService = {};
const aiService = {};
const ChatDotRound = {};
const User = {};
const ChatLineSquare = {};
const Setting = {};
const ArrowDown = {};
const Send = {};
const Plus = {};
const LogOut = {};

const component = {
  name: 'ChatHome',
  components: {
    ChatDotRound,
    User,
    ChatLineSquare,
    Setting,
    ArrowDown,
    Send,
    Plus,
    LogOut
  },
  data() {
    return {
      inputMessage: '',
      loading: false,
      creatingRoom: false,
      messages: [],
      chatRooms: [],
      currentUser: null,
      aiRoom: null,
      currentRoom: null,
      showCreateRoomDialog: false,
      roomForm: {
        name: '',
        roomType: 'private',
        memberIdsInput: ''
      }
}
      // 表单验证规则
      roomRules: {
        name: [
          { required: true, message: '请输入聊天室名称', trigger: 'blur' },
          { min: 2, max: 20, message: '聊天室名称长度应在2-20个字符之间', trigger: 'blur' }
        ],
        roomType: [
          { required: true, message: '请选择聊天室类型', trigger: 'change' }
        ],
        memberIdsInput: [
          { 
            validator: (rule, value, callback) => {
              if (this.roomForm.roomType === 'group' && !value.trim()) {
                callback(new Error('群聊必须指定成员'));
              } else if (value.trim()) {
                // 验证成员ID格式
                const ids = value.split(',').map(id => id.trim()).filter(id => id);
                for (const id of ids) {
                  if (!/^\d+$/.test(id)) {
                    callback(new Error('成员ID必须是数字'));
                    return;
                  }
                }
              }
              callback();
            }, 
            trigger: 'blur' 
          }
        ]
      },
      // AI模型相关数据
      aiModels: [],
      selectedModelId: 'phi2', // 默认选择phi2模型
      currentModel: { id: 'phi2', name: 'Phi-2', description: '微软小型语言模型', type: 'free' },
      // 模型类型定义
      modelTypes: { free: '免费模型', premium: '高级模型' },
      // UI状态
      showModelDropdown: false
    }
  },
  computed: {
    // 获取分组后的模型列表
    groupedModels() {
      return aiService.groupModelsByType(this.aiModels);
    },
    // 获取有模型的类型列表
    availableModelTypes() {
      const modelsByType = this.getModelsByType()
      return Object.keys(this.modelTypes).filter(type => modelsByType[type].length > 0)
    }
  },
  mounted() {
    this.initializeApp();
    // 监听点击外部区域关闭下拉菜单等
    document.addEventListener('click', this.handleDocumentClick);
  },
  beforeUnmount() {
    document.removeEventListener('click', this.handleDocumentClick);
  },
  methods: {
    // 初始化应用
    async initializeApp() {
      try {
        await Promise.all([
          this.loadCurrentUser(),
          this.loadChatRooms(),
          this.loadAIModels()
        ]);
        // 默认选中AI聊天室
        if (this.aiRoom) {
          this.switchRoom(this.aiRoom);
          // 加载AI聊天室的欢迎消息
          this.loadAIMessages();
        }
      } catch (error) {
        console.error('初始化应用失败:', error);
        this.$message.error('初始化失败，请刷新页面重试');
      }
    },
    
    // 加载当前用户信息
    async loadCurrentUser() {
      try {
        this.currentUser = await authService.getCurrentUser();
      } catch (error) {
        console.error('加载用户信息失败:', error);
        this.$message.error('加载用户信息失败');
        // 如果获取用户信息失败，可能是token过期，跳转到登录页
        authService.logout();
        this.$router.push('/login');
      }
    }
}
    },